package light.head.dao.el;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.el.Lecturer;

import org.springframework.stereotype.Service;

@Service
public class LecturerDAO extends BaseDAO<Lecturer, Integer>{
	private final String sql_loadAll = "FROM Lecturer c WHERE c.deleted = 0";
	
	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pageBean) {
		if(oc == null) return null;
		StringBuffer sql = new StringBuffer("FROM Lecturer l WHERE l.oc = ?");
		List<Object> list = new ArrayList<Object>();
		list.add(oc);
		if(map != null){
			String ln = map.get(Parm.LN);
			String deleted = map.get(Parm.DELETED);
			
			if(isNotNull(ln)){
				sql.append(" AND l.ln LIKE ?");
				list.add("%" + ln + "%");
			}
			if(isNotNull(deleted)){
				sql.append(" AND l.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		sql.append(" ORDER BY l.code DESC");
		return findToPageBean(sql.toString(), pageBean, list);
	}
	public List<Lecturer> getAll(){
		if (null == Cache.leList || Cache.leList.size() == 0){
			loadAll();
		}
		return Cache.leList;
	}
	
	public List<Lecturer> loadAll(){
		Cache.leList = find(sql_loadAll);
		return Cache.leList;
	}
	
	public List<Lecturer> findLc(Integer oc,Integer lc){
		if (oc == null || lc == null) return null;
		String hql = "FROM Lecturer WHERE deleted = 0 AND oc = ? and code = ?";
		return find(hql,null,null,oc,lc);
	}
	
	/**
	 * 根据条件分页查询讲师信息
	 */
	public RESULT findLc(Integer oc, Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM Lecturer l WHERE l.deleted = 0 AND l.oc =?");
		List<Object> list =  new ArrayList<Object>();
		list.add(oc);
		if (map != null) {
			String ln = map.get(Parm.LN);
			String suc = map.get(Parm.SUC);

			if (isNotNull(ln)) {
				hql.append(" AND l.ln LIKE ?");
				list.add("%" + ln + "%");
			}
			if (isNotNull(suc)) {
				hql.append(" AND l.code NOT IN (" + suc + ")");
			}
		}
		hql.append(" ORDER BY l.code");
		return findToPageBean(hql.toString(), pageBean, list);
	}
	

}
